home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / mint104s.zoo / mint.src / trutil.c < prev    next >
C/C++ Source or Header  |  1993-03-08  |  6KB  |  370 lines

  1. #define YYSTYPE char *
  2. #include "asmtrans.h"
  3. #include "asmtab.h"
  4.  
  5. #define QUOTESYM '"'
  6. #define CMDSYM '%'
  7.  
  8. FILE *infile, *outfile;
  9.  
  10. #define MAXNEST 10
  11. int hidecnt = 0;
  12. int ifstack[MAXNEST], ifstkptr;
  13.  
  14. void emit(s)
  15.     char *s;
  16. {
  17.     if (hidecnt == 0)
  18.         fputs(s, outfile);
  19.     free(s);
  20. }
  21.  
  22. char *concat(s1, s2)
  23.     char *s1, *s2;
  24. {
  25.     size_t siz = strlen(s1) + strlen(s2) + 1;
  26.     char *r;
  27.  
  28.     r = malloc(siz);
  29.     if (!r) return 0;
  30.  
  31.     strcpy(r, s1);
  32.     strcat(r, s2);
  33.     return r;
  34. }
  35.  
  36. char *concat3(s1, s2, s3)
  37.     char *s1, *s2, *s3;
  38. {
  39.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3) + 1;
  40.     char *r;
  41.  
  42.     r = malloc(siz);
  43.     if (!r) return 0;
  44.  
  45.     strcpy(r, s1);
  46.     strcat(r, s2);
  47.     strcat(r, s3);
  48.     return r;
  49. }
  50.  
  51. char *concat4(s1, s2, s3, s4)
  52.     char *s1, *s2, *s3, *s4;
  53. {
  54.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
  55.     char *r;
  56.  
  57.     r = malloc(siz);
  58.     if (!r) return 0;
  59.  
  60.     strcpy(r, s1);
  61.     strcat(r, s2);
  62.     strcat(r, s3);
  63.     strcat(r, s4);
  64.     return r;
  65. }
  66.  
  67. char *concat5(s1, s2, s3, s4, s5)
  68.     char *s1, *s2, *s3, *s4, *s5;
  69. {
  70.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3)
  71.             + strlen(s4) + strlen(s5) + 1;
  72.     char *r;
  73.  
  74.     r = malloc(siz);
  75.     if (!r) return 0;
  76.  
  77.     strcpy(r, s1);
  78.     strcat(r, s2);
  79.     strcat(r, s3);
  80.     strcat(r, s4);
  81.     strcat(r, s5);
  82.     return r;
  83. }
  84.  
  85. char *concat6(s1, s2, s3, s4, s5, s6)
  86.     char *s1, *s2, *s3, *s4, *s5, *s6;
  87. {
  88.     size_t siz = strlen(s1) + strlen(s2) + strlen(s3)
  89.             + strlen(s4) + strlen(s5) + strlen(s6) + 1;
  90.     char *r;
  91.  
  92.     r = malloc(siz);
  93.     if (!r) return 0;
  94.  
  95.     strcpy(r, s1);
  96.     strcat(r, s2);
  97.     strcat(r, s3);
  98.     strcat(r, s4);
  99.     strcat(r, s5);
  100.     strcat(r, s6);
  101.     return r;
  102. }
  103.  
  104. static int is_word_sym(c)
  105.     int c;
  106. {
  107.     if (c == '.' || (c >= '0' && c <= '9')) return 1;
  108.     if (c >= 'a' && c <= 'z') return 1;
  109.     if (c >= 'A' && c <= 'Z') return 1;
  110.     if (c == '_') return 1;
  111.     return 0;
  112. }
  113.  
  114. typedef struct wordtab {
  115.     char *word;
  116.     char *defn;
  117.     struct wordtab *next;
  118. } WORDTAB;
  119.  
  120. WORDTAB *globaltab;
  121.  
  122. void
  123. do_define(word, defn)
  124.     char *word, *defn;
  125. {
  126.     WORDTAB *w;
  127.  
  128.     w = malloc(sizeof(WORDTAB));
  129.     if (!w) return;
  130.     w->word = strdup(word);
  131.     w->defn = strdup(defn);
  132.     w->next = globaltab;
  133.     globaltab = w;
  134. }
  135.  
  136. /*
  137.  * if we were actually using this program for
  138.  * large things, we would use a hash table
  139.  * to speed up the table lookup; but for the
  140.  * uses we have, there aren't likely to be
  141.  * many defines
  142.  */
  143.  
  144. char *
  145. wordlookup(which)
  146.     char *which;
  147. {
  148.     WORDTAB *w;
  149.  
  150.     for (w = globaltab; w; w = w->next) {
  151.         if (!strcmp(which, w->word)) {
  152.             return strdup(w->defn);
  153.         }
  154.     }
  155.     return strdup(which);
  156. }
  157.  
  158. void
  159. do_ifdef(which)
  160.     char *which;
  161. {
  162.     int output = 0;
  163.     WORDTAB *w;
  164.  
  165.     for (w = globaltab; w; w = w->next) {
  166.         if (!strcmp(which, w->word)) {
  167.             output = 1;
  168.             break;
  169.         }
  170.     }
  171.     if (ifstkptr < MAXNEST) {
  172.         ifstack[ifstkptr++] = output;
  173.         if (!output)
  174.             hidecnt++;
  175.     } else {
  176.         ifstkptr++;
  177.         yyerror("too many levels of %ifdef");
  178.     }
  179. }
  180.  
  181. void
  182. do_ifndef(which)
  183.     char *which;
  184. {
  185.     int output = 1;
  186.     WORDTAB *w;
  187.  
  188.     for (w = globaltab; w; w = w->next) {
  189.         if (!strcmp(which, w->word)) {
  190.             output = 0;
  191.             break;
  192.         }
  193.     }
  194.     if (ifstkptr < MAXNEST) {
  195.         ifstack[ifstkptr++] = output;
  196.         if (!output)
  197.             hidecnt++;
  198.     } else {
  199.         ifstkptr++;
  200.         yyerror("too many levels of %ifdef");
  201.     }
  202. }
  203.  
  204. void
  205. do_else()
  206. {
  207.     int output;
  208.  
  209.     if (ifstkptr == 0) {
  210.         yyerror("%else without %ifdef");
  211.     } else {
  212.         if (ifstkptr > MAXNEST) return;
  213.     /* if we were outputting, stop */
  214.     /* otherwise, start again */
  215.         output = !ifstack[ifstkptr-1];
  216.         if (output)
  217.             hidecnt--;
  218.         else
  219.             hidecnt++;
  220.         ifstack[ifstkptr-1] = output;
  221.     }
  222. }
  223.  
  224. void
  225. do_endif()
  226. {
  227.     int output;
  228.  
  229.     if (ifstkptr == 0) {
  230.         yyerror("%endif without %ifdef");
  231.     } else {
  232.         ifstkptr--;
  233.         if (ifstkptr >= MAXNEST) return;
  234.  
  235.         output = ifstack[ifstkptr];
  236.         if (!output)
  237.             hidecnt--;
  238.     }
  239. }
  240.  
  241. /* this is a terrible hack to remove the leading
  242.  * '_' from labels...
  243.  */
  244.  
  245. char *
  246. fixupword(w)
  247.     char *w;
  248. {
  249.     if (*w == '_' && syntax == PUREC)
  250.         return strdup(w+1);
  251.     else
  252.         return strdup(w);
  253. }
  254.  
  255.  
  256. static char footext[1024];
  257.  
  258. int
  259. yylex()
  260. {
  261.     int c;
  262.     char *to = footext;
  263.     int cmdword;
  264.     static int saweoln = 1;
  265.  
  266.     c = getc(infile);
  267.  
  268.     if (c < 0) {
  269. doeof:
  270.         saweoln = 1;
  271.         return 0;
  272.     }
  273.     if (c == ';') {
  274. docomment:
  275.         if (syntax == GAS)
  276.             c = '|';
  277.         *to++ = c;
  278.         do {
  279.             c = getc(infile);
  280.             if (c < 0) return 0;
  281.             if (c != '\r')
  282.                 *to++ = c;
  283.         } while (c != '\n');
  284.         *to = 0;
  285.         yylval = strdup(footext);
  286.         saweoln = 1;
  287.         return EOLN;
  288.     }
  289.     if (c == '\n') {
  290. doeoln:
  291.         *to++ = c;
  292.         *to = 0;
  293.         yylval = strdup(footext);
  294.         saweoln = 1;
  295.         return EOLN;
  296.     }
  297.     if (c == CMDSYM && saweoln) {
  298.         cmdword = 1;
  299.         c = getc(infile);
  300.     } else {
  301.         cmdword = 0;
  302.     }
  303.  
  304.     if (c == ' ' || c == '\t' || c == '\r') {
  305.         do {
  306.             if (c == '\r')
  307.                 c = ' ';
  308.             *to++ = c;
  309.             c = getc(infile);
  310.         } while (c == ' ' || c == '\t');
  311.         if (c == '\n') goto doeoln;
  312.         if (c == ';') goto docomment;
  313.         if (!cmdword) {
  314.             ungetc(c, infile);
  315.             *to = 0;
  316.             yylval = strdup(footext);
  317.             return WHITESP;
  318.         } else {
  319.             to = footext;
  320.         }
  321.     }
  322.  
  323.     saweoln = 0;
  324.  
  325.     if (c == QUOTESYM) {
  326.         for(;;) {
  327.             c = getc(infile);
  328.             if (c < 0) goto doeof;
  329.             if (c == QUOTESYM) break;
  330.             *to++ = c;
  331.         }
  332.         *to = 0;
  333.         yylval = strdup(footext);
  334.         return STRING;
  335.     }
  336.     if (is_word_sym(c)) {
  337.         do {
  338.             *to++ = c;
  339.             c = getc(infile);
  340.         } while (is_word_sym(c));
  341.         ungetc(c, infile);
  342.         *to = 0;
  343.         if (cmdword) {
  344.             yylval = footext;
  345.             if (!strcmp(footext, "define")) {
  346.                 return DEFINECMD;
  347.             } else if (!strcmp(footext, "include")) {
  348.                 return INCLUDECMD;
  349.             } else if (!strcmp(footext, "ifdef")) {
  350.                 return IFDEFCMD;
  351.             } else if (!strcmp(footext, "ifndef")) {
  352.                 return IFNDEFCMD;
  353.             } else if (!strcmp(footext, "else")) {
  354.                 return ELSECMD;
  355.             } else if (!strcmp(footext, "endif")) {
  356.                 return ENDIFCMD;
  357.             } else {
  358.                 fprintf(stderr, "Unknown command: %s\n", footext);
  359.             }
  360.         }
  361.         yylval = fixupword(footext);
  362.         return WORD;
  363.     }
  364.  
  365.     *to++ = c;
  366.     *to = 0;
  367.     yylval = footext;
  368.     return c;
  369. }
  370.